package com.manage.user.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.base.bean.ResultBean;
import com.manage.base.controller.BaseController;
import com.manage.config.model.Config;
import com.manage.user.model.Menu;
import com.manage.user.model.Operation;
import com.manage.user.model.User;
import com.manage.user.model.result.MenuResult;
import com.manage.user.service.IUserService;

/**
 * 系统管理
 * <p>
 * <ul>
 * <li>账号管理</li>
 * <li>个人中心</li>
 * </ul>
 * <p>
 * @author dizh
 *
 */
@Controller
@RequestMapping("/user")
public class UserController
		extends BaseController {

	private static final long serialVersionUID = 1L;

	static final Logger LOG = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private IUserService userService;

	/**
	 * 登录
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public ResultBean login(String loginName, String loginPassword, HttpServletRequest request) {
		LOG.info("user login called loginName = " + loginName + " loginPassword = " + loginPassword);
		ResultBean resultBean = null;
		try {
			User user = new User();
			user.setLoginName(loginName);
			user.setLoginPassword(loginPassword);
			List<User> list = userService.select(user);
			if (list != null && list.size() > 0) {
				User temp = list.get(0);
				int user_status=temp.getUser_status();
				
				if(1==user_status){
					resultBean = new ResultBean(Config.FAILE, Config.USER_OFF_MSG);
				}
				else if (!temp.getLoginPassword().equals(user.getLoginPassword())) {
					resultBean = new ResultBean(Config.FAILE, Config.USER_ERROR_MSG);
				} else {
					hideUserAttribute(user);
					List<Menu> menus = userService.menuSelect(temp);
					List<Operation> operations = userService.operationSelect(temp);
					temp.setMenus(menus);
					temp.setOperations(operations);
					request.getSession().setAttribute(Config.USER_SESSION, temp);
					resultBean = new ResultBean(Config.SUCCESS, Config.SUCCESS_MSG);
				}
			} else {
				resultBean = new MenuResult(Config.FAILE, Config.USER_ERROR_MSG);
			}
		} catch (Exception e) {
			LOG.error("call user login error !" + e.getMessage(), e);
			resultBean = new MenuResult(Config.FAILE, Config.ERROR_MSG);
		}
		return resultBean;
	}

	/**
	 * 登出
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	@ResponseBody
	public ResultBean logout(HttpServletRequest request) {
		LOG.info("call user logout ");
		try {
			request.getSession().removeAttribute(Config.USER_SESSION);
			return new ResultBean(Config.SUCCESS, Config.SUCCESS_MSG);
		} catch (Exception e) {
			LOG.error("call user logout error !" + e.getMessage(), e);
			return new ResultBean(Config.FAILE, Config.ERROR_MSG);
		}
	}

	@RequestMapping(value = "menu/select", method = RequestMethod.GET)
	@ResponseBody
	public List<Menu> menuSelect(HttpServletRequest request, String data) {
		LOG.info("call menuSelect data = " + data);
		try {
			Object obj = request.getSession().getAttribute(Config.USER_SESSION);
			User user = (User) obj;
			List<Menu> menus = userService.menuSelect(user);
			List<Menu> parents = new ArrayList<Menu>();
			for (Menu m : menus) {
				if (m.getParentId() == -1) {
					parents.add(m);
				}
			}

			List<Menu> result = new ArrayList<Menu>();
			for (Menu p : parents) {
				result.add(p);
				for (Menu m : menus) {
					if (m != p && m.getParentId() == p.getMenuId()) {
						result.add(m);
					}
				}
			}
			return result;
		} catch (Exception e) {
			LOG.error("call Menu Select error !" + e.getMessage(), e);
			return null;
		}
	}

	@RequestMapping(value = "select", method = RequestMethod.GET)
	@ResponseBody
	public User select(HttpServletRequest request, String data) {
		LOG.info("call user select data = " + data);
		try {
			Object obj = request.getSession().getAttribute(Config.USER_SESSION);
			User user = (User) obj;
			return user;
		} catch (Exception e) {
			LOG.error("call User Select error !" + e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 隐藏session中用户的某些属性
	 * 
	 * @param user
	 */
	private void hideUserAttribute(User user) {
		if (user != null) {
			if (user.getLoginPassword() != null && user.getLoginPassword().length() > 0) {
				user.setLoginPassword(null);
			}
		}
	}
}
